package com.hyj.ecp.backstage.sys.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.ecp.goods.GoodsTypeQO;
import com.hyj.ecp.backstage.sys.bean.qo.ConfigQO;
import com.hyj.ecp.backstage.sys.bean.qo.LoginQO;
import com.hyj.ecp.backstage.sys.bean.vo.User;
import com.hyj.ecp.backstage.sys.service.IEcpLoginService;
import com.hyj.ecp.backstage.sys.service.impl.EcpLoginService;
import com.hyj.ecp.common.constant.SysConstant;



@Controller
@SessionAttributes("user")
@RequestMapping("/backstage/sys/*")
public class EcpLoginAction {

	Log log = LogFactory.getLog(EcpLoginAction.class);
	@Autowired
    private IEcpLoginService service;
	
	
	/**
	 * 默认 是request set  map 的所有属性值
	 * @param u
	 * @param map
	 * @return
	 */
    @RequestMapping(value="login.do")
    public String login(LoginQO u,ModelMap map)
    {
		log.info("login.do start..");
		User user = service.login(u);
		if (user != null) {
			map.put("user", user);

			// 获取菜单
			u.setRoleId(user.getRoleId());
			List<Map<String, Object>> munues = service.queryMenue(u);
			map.put("menue", munues);
			return "view/backmail";
		} else {
			map.put("info", "用户名密码错误！");
			return "login";
		}
    }
    
    @RequestMapping(value="quit.do")
    public String quit(LoginQO u,ModelMap map)
    {
    	//log.info(">>"+map.get("user"));
    	log.info("hello quit.do");
    	map.remove("user");
    	//log.info(">>"+map.get("user"));
    	return "login";
    }
    
    
    @ResponseBody
    @RequestMapping(value="updatePassword.do")
    public Object updatePassword(LoginQO qo,ModelMap map)
    {
      log.info("hello modifyPassword.do");
      Map<String,Object> vo = new HashMap<String,Object>();
      try{
    	int a = service.updatePassword(qo);
    	if(a>0){
    		vo.put("returnCode", 0);
    		vo.put("saveNum", a);
    		vo.put("msg", SysConstant.UPDATE_SUCCESS_STR);
    	}else{
    		vo.put("returnCode", 999);
    		vo.put("saveNum", a);
    		vo.put("msg", SysConstant.UPDATE_FAIL_STR);
    	}
      }catch(Exception e){
    	 log.error("addtypePage.do",e);
      	 vo.put("returnCode", 999);
  		 vo.put("saveNum", 0);
  		 vo.put("msg", SysConstant.UPDATE_FAIL_STR);
      }
    	return vo;
    }
    
    @RequestMapping(value="tomodifypage.do")
    public ModelAndView test(LoginQO u)
    {
    	ModelAndView  view = new ModelAndView("view/backstage/sys/modifyPsd");
    	return view;
    }
    
    /**
     * 获取菜单
     * @param u
     * @return
     */
    @ResponseBody
    @RequestMapping(value="menu.do")
    public Object menu(LoginQO u)
    {
    	List<Map<String,Object>> munues = service.queryMenue(u);
    	return munues;
    }
    
  
    /**
     * 贵就赔
     * @param qo
     * @param map
     * @return
     * @throws IOException
     */
    @RequestMapping(value="toGjpPage.do")
    public String togoodstype(ConfigQO qo,ModelMap map) throws IOException
    {
    	log.info("toGjpPage.do");
    	qo.setType("goodssale");
    	qo.setKey("gjp");
    	ConfigQO vo  = service.queryConfig(qo);
    	map.put("vo", vo);
		return "view/backstage/sys/gjp";
    }
    
    /**
     * 客户服务
     * @param qo
     * @param map
     * @return
     * @throws IOException
     */
    @RequestMapping(value="toKhfwPage.do")
    public String toKhfwPage(ConfigQO qo,ModelMap map) throws IOException
    {
    	log.info("toKhfwPage.do ...  ");
    	qo.setType("goodssale");
    	qo.setKey("khfw");
    	ConfigQO vo  = service.queryConfig(qo);
    	map.put("vo", vo);
		return "view/backstage/sys/khfw";
    }
    
    /**
     * 常见问题
     * @param qo
     * @param map
     * @return
     * @throws IOException
     */
    @RequestMapping(value="toCjwtPage.do")
    public String toCjwtPage(ConfigQO qo,ModelMap map) throws IOException
    {
    	log.info("toCjwtPage.do ...  ");
    	qo.setType("goodssale");
    	qo.setKey("cjwt");
    	ConfigQO vo  = service.queryConfig(qo);
    	map.put("vo", vo);
		return "view/backstage/sys/khfw";
    }
    
    /**
     * 价格保护
     * @param qo
     * @param map
     * @return
     * @throws IOException
     */
    @RequestMapping(value="toJgbhPage.do")
    public String toJgbhPage(ConfigQO qo,ModelMap map) throws IOException
    {
    	log.info("toJgbhPage.do  价格保护  ...  ");
    	qo.setType("goodssale");
    	qo.setKey("jgbh");
    	ConfigQO vo  = service.queryConfig(qo);
    	map.put("vo", vo);
		return "view/backstage/sys/jgbh";
    }
    
    
    /**
     * 系统配置参数查询接口
     */
    @ResponseBody
    @RequestMapping(value="queryConfig.do")
    public Object queryConfig(ConfigQO qo)
    {
    	ConfigQO vo  = service.queryConfig(qo);
    	return vo;
    }
    
    /**
     * 系统配置参数修改接口
     */
    @ResponseBody
    @RequestMapping(value="updateConfig.do")
    public Object updateGjp(ConfigQO qo)
    {
    	//qo.setKey("");
    	//qo.setValue("");
    	//qo.setType("goodssale");
    	Map<String,Object> vo = new HashMap<String,Object>();
    	try{
    	int a  = service.updateConfig(qo);
    	if(a>0){
    		vo.put("returnCode", 0);
    		vo.put("saveNum", a);
    		vo.put("msg", "保存成功！");
    	}else{
    		vo.put("returnCode", 999);
    		vo.put("saveNum", a);
    		vo.put("msg", "保存失败！");
    	}
    	}catch(Exception e){
    		log.error("addtypePage.do",e);
        	vo.put("returnCode", 999);
    		vo.put("saveNum", 0);
    		vo.put("msg", "保存失败！");
    	}
    	return vo;
    }
}
